﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using RbacPermissionManage.Application.Admins.Dto;
using RbacPermissionManage.Application.Roles.Dto;
using RbacPermissionManage.Common;
using RbacPermissionManage.DoMain;
using RbacPermissionManage.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace RbacPermissionManage.Application.Roles
{
    public class RoleServices : IRoleServices
    {
        private readonly IRepository<Role> repository;
        private readonly IMapper mapper;

        public RoleServices(IRepository<Role> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="createDto"></param>
        /// <returns></returns>
        public async Task<ResultDto> CreateAsync(RoleCreateDto createDto)
        {
            //查用户名
            var isExists = await repository.FirstOrDefaultAsync(x => x.RoleName == createDto.RoleName);
            if (isExists != null)
            {
                return new ResultDto { Code = Code.Failed, Message = "角色名称已使用" };
            }

            Role role = mapper.Map<RoleCreateDto, Role>(createDto);
            try
            {
                await repository.InsertAsync(role);//调仓储的添加方法
                return new ResultDto { Code = Code.Success };
            }
            catch (Exception e)
            {
                return new ResultDto { Code = Code.Failed, Message = e.Message };
            }
        }

        /// <summary>
        /// 显示查询分页角色列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<ResultDto<(int, List<RoleListDto>)>> GetRoleAsync(SearchDto searchDto, int pageIndex, int pageSize)
        {
            var list = repository.Queryable().Where(x => x.IsDel == false);
            if (!string.IsNullOrEmpty(searchDto.Keywords))
            {
                list = list.Where(x => x.RoleName.Contains(searchDto.Keywords) || x.RoleCode.Contains(searchDto.Keywords));
            }
            var PageData = await list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            var result = mapper.Map<List<Role>, List<RoleListDto>>(PageData);
            return new ResultDto<(int, List<RoleListDto>)> { Data = (await list.CountAsync(), result) };
        }

        /// <summary>
        /// 根据Id获取roleDto
        /// </summary>
        /// <param name="RoleId">角色Id</param>
        /// <returns></returns>
        public async Task<ResultDto<RoleListDto>> BackfillAsync(int RoleId)
        {
            var role = await repository.FindAsync(RoleId);
            return new ResultDto<RoleListDto> { Code = Code.Success, Data = mapper.Map<Role, RoleListDto>(role) };
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="updateDto"></param>
        /// <returns></returns>
        public async Task<ResultDto> UpdateAsync(RoleUpdateDto updateDto)
        {
            //查用户名
            var isExists = await repository.FirstOrDefaultAsync(x => x.RoleName == updateDto.RoleName && x.RoleId != updateDto.RoleId);
            if (isExists != null)
            {
                return new ResultDto { Code = Code.Failed, Message = "角色名称已使用" };
            }
            Role entity = await repository.FindAsync(updateDto.RoleId);
            mapper.Map(updateDto, entity);
            try
            {
                await repository.UpdateAsync(entity);//调仓储的添加方法
                return new ResultDto { Code = Code.Success };
            }
            catch (Exception e)
            {
                return new ResultDto { Code = Code.Failed, Message = e.Message };
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteAsync(int RoleId)
        {
            try
            {
                await repository.DeleteAsync(RoleId);
                return new ResultDto<bool> { Code = Code.Success, Data = true };
            }
            catch (Exception e)
            {
                return new ResultDto<bool> { Code = Code.Failed, Data = false, Message = e.Message };
                throw;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> BatchDelAsync(List<int> roleIds)
        {
            try
            {
                await repository.DeleteAsync(roleIds);
                return new ResultDto<bool> { Code = Code.Success, Data = true };
            }
            catch (Exception e)
            {
                return new ResultDto<bool> { Code = Code.Failed, Data = false, Message = e.Message };
                throw;
            }
        }
    }
}
